package com.sy.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sy.context.BaseContext;
import com.sy.dto.CoursePageQueryDTO;
import com.sy.pojo.Course;
import com.sy.pojo.CourseUser;
import com.sy.properties.JwtProperties;
import com.sy.result.PageResult;
import com.sy.result.Result;
import com.sy.service.CourseService;
import com.sy.service.CourseUserService;
import com.sy.vo.CourseDetailVO;
import com.sy.vo.CourseIndexVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户端-首页课程查询
 */
@Api(tags = "用户端-首页课程查询")
@RestController("userCourseController")
@RequestMapping("/user/course")
@Slf4j
public class CourseController {

    @Autowired
    private CourseService courseService;

    @Autowired
    private CourseUserService courseUserService;

    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 用户端-首页课程查询
     * @return
     */
    @ApiOperation("用户端-首页课程查询")
    @GetMapping("/findAll")
    @Cacheable(value = "courseCache", key = "'indexCourses'",unless = "#result == null")
    public Result<List<CourseIndexVO>> findAll() {
        //调用业务层实现分页查询
        List<CourseIndexVO> allSales = courseService.findAllSales();
        return Result.success(allSales);
    }

    /**
     * 用户端-全部课程查询
     * @param coursePageQueryDTO
     * @return
     */
    @ApiOperation("用户端-全部课程查询")
    @GetMapping("/findAllCourse")
    public Result<PageResult> findAllCourse(CoursePageQueryDTO coursePageQueryDTO) {
        //打印日志
        log.info("用户端-全部课程查询:{}", coursePageQueryDTO);
        //调用业务层实现分页查询
        PageResult pageResult = courseService.findAllCourse(coursePageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 用户端-查看课程详情
     * @param cid
     * @return
     */
    @ApiOperation("用户端-查看课程详情")
    @GetMapping("/getCourseDetail")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "cid", value = "课程cid",
                    required = true, dataTypeClass = Integer.class)
    )
    public Result getCourseDetail(Integer cid) {
        //打印日志
        log.info("用户端-查看课程详情:{}", cid);
        //调用业务层实现分页查询
        CourseDetailVO courseDetailVO = courseService.getCourseDetail(cid);
        return Result.success(courseDetailVO);
    }

    /**
     * 用户端-判断用户端有没有被用户购买
     * @param cid
     * @return
     */
    @ApiOperation("用户端-判断用户端有没有被用户购买")
    @GetMapping("/auth/ifShoppingCourse")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "cid", value = "课程cid",
                    required = true, dataTypeClass = Integer.class)
    )
    public Result ifShoppingCourse(Integer cid) {
        //打印日志
        log.info("用户端-获取当前所购买课程的cid:{}", cid);
        //获取当前用户的uid
        Long uid = BaseContext.getCurrentId();
        // uid=37  and  cid = 34
        LambdaQueryWrapper<CourseUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseUser::getCid, cid)
                .eq(CourseUser::getCid, uid)    ;
        CourseUser courseUser = courseUserService.getOne(queryWrapper);
        //判断CourseUser是否为空
        if (courseUser != null) {
            return Result.success();
        }else {
            return Result.error("没有购买此课程...");
        }
    }

    /**
     * 用户端-课程中心
     * @return
     */
    @ApiOperation("用户端-课程中心")
    @GetMapping("/auth/getCourseByUid")
    public Result getCourseByUid() {
        try {
            //根据用户的id查询当前用户所购买的课程信息
            Long userId = BaseContext.getCurrentId();
            //先根据用户id 查询课程对应的id
            LambdaQueryWrapper<CourseUser> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(CourseUser::getUid, userId)
                    .select(CourseUser::getCid);
            List<Integer> cidList = courseUserService.listObjs(queryWrapper)
                    .stream().map(iter -> (Integer) iter)
                    .collect(Collectors.toList());
            //System.out.println("cidList:" + cidList);
            //根据课程cid 查询课程表信息
            List<Course> courseList = courseService.listByIds(cidList);
            return Result.success(courseList);
        } catch (Exception e) {
            throw new RuntimeException("查询失败");
        }
    }
}
